const Db = require('../services/mysql')
const { Fb, Moment, Op, Dev } = require('./utils')
const _ = require('lodash')

class Attractions {
  constructor() {}
  /**
   * 新增景区
   * @param {*} where
   */
  create(form) {
    return Db.Attractions.create({
      linename: form.linename,
      province: form.province,
      areaid: form.areaid,
      img: form.img,
      price: form.price,
      remark: form.remark,
      delstatus: '否'
    })
  }

  /**
   * 修改景区信息
   * @param {*} where
   */
  update(form) {
    return Db.Attractions.update(
      {
        linename: form.linename,
        province: form.province,
        areaid: form.areaid,
        img: form.img,
        price: form.price,
        remark: form.remark
      },
      {
        where: {
          id: form.id
        }
      }
    )
  }

  /**
   * 查找景区列表
   * @param {*} where
   */
  select(form, nowPage, pageSize) {
    let linename = form.linename || ''
    let province = form.province || ''
    let areaid = form.areaid || ''
    return Db.Attractions.findAndCountAll({
      where: {
        linename: {
          [Op.like]: '%' + linename + '%'
        },
        province: {
          [Op.like]: '%' + province + '%'
        },
        areaid: {
          [Op.like]: '%' + areaid + '%'
        }
      },
      include: [Db.Kcrecord, Db.Address, Db.City],
      //   order: [['savetime', 'DESC']],
      offset: pageSize * (nowPage - 1),
      limit: pageSize
    })
  }

  /**
   * 新闻详情
   * @param {*} where
   */
  details(id) {
    return Db.Attractions.findById(id)
  }

  /**
   * 删除景点
   * @param {*} where
   */
  delete(from) {
    return Db.Attractions.destroy({
      where: {
        id: from.id
      }
    })
  }

  /**
   * 景点删除库存
   * @param {*} where
   */
  delLibrary(from) {
    return Db.Kcrecord.destroy({
      where: {
        pid: from.id
      }
    })
  }

  /**
   * 景点创建库存
   * @param {*} where
   */
  createLibrary(id) {
    return Db.Kcrecord.create({
      pid: id,
      num: 0
    })
  }

  /**
   * 景点查询库存
   * @param {*} where
   */
  celectLibrary(id) {
    return Db.Kcrecord.findOne({
      where: {
        pid: id
      }
    })
  }

  /**
   * 景点添加库存
   * @param {*} where
   */
  addLibrary(form) {
    let num = parseInt(form.num)
    return new Promise((resolve, reject) => {
      Db.Kcrecord.findOne({
        where: {
          pid: form.pid
        }
      })
        .then(kcrecordDb => {
          kcrecordDb
            .increment(['num'], { by: num })
            .then(data => {
              // data.num += num;
              resolve(data)
            })
            .catch(err => {
              reject(err)
            })
        })
        .catch(err => {
          reject(err)
        })
    })
  }

  /**
   * 景点减少库存
   * @param {*} where
   */
  downLibrary(form) {
    let num = parseInt(form.num)
    return new Promise((resolve, reject) => {
      Db.Kcrecord.findOne({
        where: {
          pid: form.pid
        }
      })
        .then(kcrecordDb => {
          kcrecordDb
            .increment(['num'], { by: -num })
            .then(data => {
              // data.num += num;
              resolve(data)
            })
            .catch(err => {
              reject(err)
            })
        })
        .catch(err => {
          reject(err)
        })
    })
  }

  /**
   * 获取轮播图地址
   * @param {*} where
   */
  imgSelect() {
    return Db.Carousel.findAll()
  }
}

module.exports = new Attractions()
